// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIInstrumentListExpo.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.UI.Forms
{
    #region Using Directives

    using System;
    using System.ComponentModel;

    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.UI.Forms;

    #endregion

    /// <summary>
    /// The ui instrument list.
    /// </summary>
    public partial class UIInstrumentListExpo : UIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The expotype.
        /// </summary>
        private EExpoCellType expotype;

        /// <summary>
        /// The instruments.
        /// </summary>
        private IInstrument[] instruments;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIInstrumentListExpo"/> class. 
        /// Initializes a new instance of the <see cref="UIInstrumentStaticDataList"/> class.
        /// </summary>
        public UIInstrumentListExpo()
        {
            this.InitializeComponent();

            var enumValues = Enum.GetValues(typeof(EExpoCellType));
            foreach (var enumValue in enumValues)
            {
                this.ExpositionTypeCombobox.Items.Add(enumValue);
            }

            this.ExpositionTypeCombobox.SelectedIndex = 0;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Instruments.
        /// </summary>
        [Browsable(false)]
        public IInstrument[] Instruments
        {
            get
            {
                return this.instruments;
            }

            set
            {
                this.instruments = value;
            }
        }

        /// <summary>
        /// Gets UIVirtualGrid.
        /// </summary>
        [Browsable(false)]
        public UIVirtualGrid UIVirtualGrid
        {
            get
            {
                return this.virtualGrid;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The exposition type combobox_ selected index changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ExpositionTypeComboboxSelectedIndexChanged(object sender, EventArgs e)
        {
            this.expotype = (EExpoCellType)this.ExpositionTypeCombobox.SelectedItem;
        }

        /// <summary>
        /// The virtualGrid_ ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGridUIGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
            if (e.RowIndex >= this.instruments.Length || e.RowIndex < 0)
            {
                return;
            }

            var i = this.instruments[e.RowIndex];

            // According the column display required data
            if (e.ColumnIndex == 0)
            {
                e.Value = i.ToString();
                e.ToolTip = i.ToString();
                return;
            }

            // Expo columns
            var expo = i.Expo[this.expotype];
            if (expo == null)
            {
                e.Value = "-";
                return;
            }

            // According the column display required data
            switch (e.ColumnIndex)
            {
                case 1: // Buy average
                    e.Value = this.Round(i, expo.BuyAverageExecutedPrice);
                    e.ToolTip = string.Empty + expo.BuyAverageExecutedPrice;
                    break;

                case 2: // Buy qty
                    e.Value = expo.BuyExecutedQty;
                    break;

                case 3: // Sell average
                    e.Value = this.Round(i, expo.SellAverageExecutedPrice);
                    e.ToolTip = string.Empty + expo.SellAverageExecutedPrice;
                    break;

                case 4: // Sell qty 
                    e.Value = expo.SellExecutedQty;
                    break;

                case 5: // Inventory 
                    e.Value = expo.Inventory;
                    break;

                case 6: // Average Price
                    e.Value = this.Round(i, expo.AveragePrice);
                    e.ToolTip = string.Empty + expo.AveragePrice;
                    break;

                case 7: // Nominal
                    e.Value = this.Round(i, expo.NominalExpo);
                    e.ToolTip = string.Empty + expo.NominalExpo;
                    break;

                case 8: // Realized
                    e.Value = this.Round(i, expo.Realized);
                    e.ToolTip = string.Empty + expo.Realized;
                    break;

                case 9: // Unrealized
                    e.Value = this.Round(i, expo.UnRealized);
                    e.ToolTip = string.Empty + expo.UnRealized;
                    break;

                case 10: // Fees
                    e.Value = this.Round(i, expo.FeesTracker.Fees);
                    e.ToolTip = string.Empty + expo.FeesTracker.Fees;
                    break;

                case 11: // PNL
                    e.Value = this.Round(i, expo.PL);
                    e.ToolTip = string.Empty + expo.PL;
                    break;

                case 12: // Net PNL
                    e.Value = this.Round(i, expo.NetPL);
                    e.ToolTip = string.Empty + expo.NetPL;
                    break;
            }
        }

        /// <summary>
        /// The virtualGrid_ ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGridUIGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
            if (this.instruments == null)
            {
                e.RowCount = 0;
                return;
            }

            e.RowCount = this.instruments.Length;
        }

        /// <summary>
        /// The round.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <param name="price">
        /// The price.
        /// </param>
        /// <returns>
        /// The round.
        /// </returns>
        private string Round(IInstrument i, double price)
        {
            var ticks = i.InstrumentStaticData.TickSizeRule.Tick(Math.Abs(price));
            return ForgePrettyPrinter.Prices.Format(price, ticks / 10);
        }

        #endregion
    }
}